package com.horoscope.astrology.zodiac.signs.free.adapter

import android.content.Context
import android.os.Build
import android.text.Html
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.FrameLayout
import android.widget.LinearLayout
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView
import com.horoscope.astrology.zodiac.signs.free.AppSettingManager
import com.horoscope.astrology.zodiac.signs.free.R
import com.horoscope.astrology.zodiac.signs.free.bean.BirthContentBean
import com.horoscope.astrology.zodiac.signs.free.utils.HtmlToText
import com.horoscope.astrology.zodiac.signs.free.utils.SysUtils

class ContentAdapter(private val mContext: Context) : BaseAdapter() {

    private var mName = ""
    private val mData = ArrayList<BirthContentBean>()

    fun setData(list: ArrayList<BirthContentBean>) {
        mData.clear()
        mData.addAll(list)
        notifyDataSetChanged()

    }

    fun setName(name: String) {
        mName = name
    }


    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder {
        val view = LayoutInflater.from(mContext).inflate(R.layout.item_chart_content, parent, false)

        return ContentViewHolder(view)
    }

    override fun getItemCount(): Int {
        return mData.size
    }

    private var targetS = ""
    override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {
        when (holder) {
            is ContentViewHolder -> {
                val bean = mData[position]
                val title = HtmlToText.delHTMLTag(bean.title).trim()
                if (bean.content.length > 500 && needAD(bean.title)) {
                    targetS = bean.content.substring(0, 500)
                    holder.mask.visibility = View.VISIBLE
                } else if (bean.content.isEmpty()) {
                    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
                        holder.title.text = Html.fromHtml(bean.title, 0)
                    } else {
                        holder.title.text = Html.fromHtml(bean.title)
                    }
                    holder.content.visibility = View.GONE
                    holder.mask.visibility = View.GONE
                    holder.video.visibility = View.GONE
                    holder.root.setPadding(0, 0, 0, SysUtils.rp(10f))
                } else {
                    targetS = bean.content
                    holder.mask.visibility = View.INVISIBLE
                }
                // Html.fromHtml() 富文本展示（简单的HTML标签）
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
                    holder.title.text = Html.fromHtml(title, 0)
                    holder.content.text = Html.fromHtml(HtmlToText.delHTMLTag(targetS), 0)
                } else {
                    holder.title.text = Html.fromHtml(title)
                    holder.content.text = Html.fromHtml(HtmlToText.delHTMLTag(targetS))
                }

                // 点击显示激励广告 并 保存
                holder.video.setOnClickListener {
                    if (!SysUtils.isFastClick()) {
                        mOnItemClickedListener!!.onItemClick(it, position, "")
                    }
                }
                /* val resID = ConstellationUtil.getIconByName(mName)
                 if(resID !=null){
                     holder.icon.setImageResource(resID)
                 }*/

            }
        }
    }

    private fun needAD(key: String): Boolean {
        return AppSettingManager.readBoolean(key, true)

    }

    fun notifyPositionData(position: Int) {
        AppSettingManager.saveBoolean(false, mData[position].title)
        notifyItemChanged(position)
    }

    inner class ContentViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
        val root: LinearLayout = itemView.findViewById(R.id.ll_root)
        val title: TextView = itemView.findViewById(R.id.text_View_title)
        val content: TextView = itemView.findViewById(R.id.text_view_content)
        val mask: FrameLayout = itemView.findViewById(R.id.frame_mask)
        val video: LinearLayout = itemView.findViewById(R.id.linear_watch_video)
        //val icon = itemView.findViewById<ImageView>(R.id.image_view_chart_icon)
    }
}